var form = null;
layui.use(['table', 'element', 'form', 'laydate', "code"], function () {
    form = layui.form;
});

$(function () {
    // 初始化编辑项目值
    var editProjectEditType = getCookie("edit-project-edit-type")
    // 获取flinkVersion
    getFlinkVersion(editProjectEditType)

    // 新增
    if (editProjectEditType === '1') {
        $("#id").val(0);
        $("#gateway_num").val(null);

        $("#route_id").val(null);
        $("#route_uri").val(null);
        $("#route_predicates").val(null);
        $("#route_filters_stripprefix").val(null);

        $("#project_summary").val(null);
        $("#project_detail").val(null);

        $("#flink_version").val(null);
        $("#start_class").val(null);
        $("#task_manager_memory").val(null);
        $("#start_jar_name").val(null);
        $("#image_name").val(null);
    }

    // 编辑
    if (editProjectEditType === '2') {
        $("#id").val(getCookie("edit-project-id"));
        $("#gateway_num").val(getCookie("edit-project-gateway-num"));

        $("#route_id").val(getCookie("edit-project-route-id"));
        $("#route_uri").val(getCookie("edit-project-route-uri"));
        $("#route_predicates").val(getCookie("edit-project-route-predicates"));
        $("#route_filters_stripprefix").val(getCookie("edit-project-route-filters-stripprefix"));

        $("#project_summary").val(getCookie("edit-project-project-summary"));
        $("#project_detail").val(getCookie("edit-project-project-detail"));

        $("#flink_version").val(getCookie("edit-project-flink-version"));
        $("#start_class").val(getCookie("edit-project-start-class"));
        $("#task_manager_memory").val(getCookie("edit-project-task-manager-memory"));
        $("#start_jar_name").val(getCookie("edit-project-start-jar-name"));
        $("#image_name").val(getCookie("edit-project-image-name"));
    }
});

/**
 * 获取flink存在的版本信息
 */
function getFlinkVersion(editProjectEditType) {
    $.ajax({
        type: "POST",
        url: "/get-flink-version",
        contentType: "application/json",
        data: JSON.stringify({}),
        success: function (req) {
            $("#flink_version").html(req.flinkVersion);
            if (editProjectEditType === '1') {
                $("#flink_version").val(null);
            }
            if (editProjectEditType === '2') {
                $("#flink_version").val(getCookie("edit-project-flink-version"));
            }
        }
    });
}

function supplementRouteInfo() {
    var route_id = $("#route_id").val()
    if (route_id.length < 1) {
        return
    }

    $("#route_uri").val("http://" + route_id + "-rest:8081")
    $("#route_predicates").val("/" + route_id + "/**")
    $("#route_filters_stripprefix").val("1");
    form.render();
}

function save_config() {
    var config = {
        'id': $("#id").val(),
        'gatewayNum': $("#gateway_num").val(),

        'routeId': $("#route_id").val(),
        'routeUri': $("#route_uri").val(),
        'routePredicates': $("#route_predicates").val(),
        'routeFiltersStripprefix': $("#route_filters_stripprefix").val(),

        'projectSummary': $("#project_summary").val(),
        'projectDetail': $("#project_detail").val(),

        'flinkVersion': $("#flink_version").val(),
        'startClass': $("#start_class").val(),
        'taskManagerMemory': $("#task_manager_memory").val(),
        'startJarName': $("#start_jar_name").val(),
        'imageName': $("#image_name").val()
    }

    var load = layer.load(1, {shade: [0.1, '#000']});
    $.ajax({
        type: "POST",
        url: "/flink_config/edit",
        contentType: "application/json",
        data: JSON.stringify(config),
        success: function (req) {
            layer.close(load);
            if (req === 'success') {
                layer.msg('操作成功');
                window.setTimeout(function () {
                    xadmin.close();
                    parent.refresh();
                }, 1000);
            } else {
                layer.msg(req);
            }
        }
    });
}

// msg 参数描述，targetId 指定控件ID
function parameterDescribe(msg, targetId) {
    layer.tips(msg, targetId, {"tips": [3, '#000000']});
}

function checkNumberBase(obj) {
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g, "");
}

function checkInteger(obj) {
    checkNumberBase(obj)
    checkRange(obj)
    //先把非数字的都替换掉，除了数字
    obj.value = obj.value.replace(/[^\d]/g, "");
}

function checkDouble(obj) {
    checkNumberBase(obj)
    checkRange(obj)
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g, "");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g, ".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}

function checkRange(obj) {
    let minValue = parseFloat(obj.getAttribute("minValue"));
    let maxValue = parseFloat(obj.getAttribute("maxValue"));
    if (null != minValue && null != maxValue) {
        if ("" !== obj.value) {
            if (obj.value < minValue) {
                obj.value = minValue
            }
            if (obj.value > maxValue) {
                obj.value = maxValue
            }
        }
    }
}